<template>
  <el-upload
    class="upload"
    ref="upload"
    action="/admin/upload/pic"
    accept="image/*"
    :limit="limit"
    :file-list="fileList"
    list-type="picture"
    :show-file-list="showList"
    :on-success="successHandle"
    :on-remove="removeHandle"
  >
    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
  </el-upload>
</template>

<script>
export default {
  name: 'upload',
  props: {
    value: {
      type: String
    },
    limit: {
      type: Number,
      default: 1
    },
    showList: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    fileList() {
      if (this.value) {
        return [
          {
            name: this.value.match(/download\/(.+)/)
              ? this.value.match(/download\/(.+)/)[1]
              : this.value,
            url: this.value
          }
        ];
      } else {
        return [];
      }
    }
  },
  methods: {
    successHandle(res) {
      this.$emit('input', res.data.url);
      this.$emit('on-upload', res.data.url);
    },
    removeHandle() {
      this.$emit('input', null);
    }
  }
};
</script>

<style lang="less">
.upload {
  .el-upload__tip {
    color: #bbb;
  }
}
</style>
